package com.idreems.AMapLocation;

import java.util.List;

import android.content.Context;
import android.location.Address;
import com.amap.api.search.geocoder.Geocoder;

public class GeocoderManager {
    private static final int MAX_RESULTS = 3;
    private static GeocoderManager sGeocoderManager;
    private Geocoder mGeocoder;

    public synchronized static GeocoderManager sharedInstance(Context context) {
        if (sGeocoderManager == null) {
            sGeocoderManager = new GeocoderManager(context);
        }
        return sGeocoderManager;
    }
    private GeocoderManager() {
    }
    private GeocoderManager(Context context) {
        mGeocoder = new Geocoder(context);
    }

    // 逆地理编码
    public void getAddress(final double mlat, final double mLon,final AddressListener listener) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    List<Address> address = mGeocoder.getFromLocation(mlat, mLon, MAX_RESULTS);
                    if (address != null && address.size() > 0) {
                        // Address addres = address.get(0);
                        if (listener!=null) {
                            listener.onSuccess(address.get(0));
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }).start();
    }

    // 地理编码
    public void getLatlon(final String name,final AddressListener listener) {
        new Thread(new Runnable() {
            public void run() {
                try {
                    List<Address> address = mGeocoder.getFromLocationName(name, 3);
                    if (address != null && address.size() > 0) {
                        // addressName = addres.getLatitude() + "," + addres.getLongitude();
                        if (listener!=null) {
                            listener.onSuccess(address.get(0));
                        }
                    }
                } catch (Exception e) {
                }

            }
        }).start();
    }
}
